﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using cs499.Database;

namespace cs499.Reports
{
    public partial class ReportsDefault : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            LinkButton reportsMenuButton = (LinkButton)Master.FindControl("LoginView").FindControl("ReportsMenuButton");
            reportsMenuButton.CssClass = "selected";
        }

        protected void exportReportbutton_Click(object sender, EventArgs e)
        {

            System.Text.StringBuilder exportFile = new System.Text.StringBuilder();


            int departmentId = (int)HttpContext.Current.Session["CurrentDepartmentId"];
            cs499.Database.Department department = Database.ListOfDepartment.GetDepartment(ID: departmentId);
            
            int placeId = Convert.ToInt32(PlaceDropDownList.SelectedValue);
            int facultyId = Convert.ToInt32(UserDropDownList.SelectedValue);

            //add filter info
            exportFile.AppendLine("#FILTER");
            exportFile.AppendLine("#Department: " + department.Name);
            exportFile.AppendLine("#Location: " + PlaceDropDownList.SelectedItem.Text);
            exportFile.AppendLine("#Faculty: " + UserDropDownList.SelectedItem.Text);
            exportFile.AppendLine("");

            //add field name line
            exportFile.AppendLine("#Course Number,Section Number,Room,TimeSlot,TaughtBy");

            
            foreach (CourseInstance courseInstance in Department.GetScheduledCourses(departmentId, placeId, facultyId))
            {
                exportFile.AppendLine(courseInstance.Course.ToString() + "," +
                                      courseInstance.SectionNumber.ToString() + "," +
                                      courseInstance.Room.ToString() + "," +
                                      courseInstance.TimeSlot.ToString() + "," +
                                      courseInstance.TaughtBy.ToString());
            }

            Response.ContentType = @"application/x-msdownload";
            Response.AppendHeader("content-disposition", "attachment; filename=ScheduleReport.csv");

            Response.Write(exportFile.ToString());
            Response.Flush();
            Response.End();
        }
    }
}